<?php
//tag_actions.php

    function php($t_array, $action='', $contrib='') {
        if($t_array[0]) {
            for($loop_index = 0; $loop_index < count($t_array); $loop_index++) {
                if ($action=='install') {
                    $output.="<div class=\"section\"><font class=\"section-title\">".PHP_INSTALL_TEXT.
                                        "</font><p class=\"php_code\">".nl2br(htmlentities(linebreak_fixing($t_array[$loop_index]['INSTALL'][0]['#'])))."</p></div>";
                    eval($t_array[$loop_index]['INSTALL'][0]['#']);
                } elseif ($action=='remove') {
                    $output.="<div class=\"section\"><font class=\"section-title\">".PHP_REMOVE_TEXT.
                                        "</font><p class=\"php_code\">".nl2br(htmlentities($t_array[$loop_index]['REMOVE'][0]['#']))."</p></div>";
                    eval($t_array[$loop_index]['REMOVE'][0]['#']);
                }
            }
        }
        return $output;
    }

    function make_dir($t_array, $action='', $contrib='') {
        if($t_array[0]) {
            for($loop_index = 0; $loop_index<count($t_array); $loop_index++) {
                if ($action=='install') {
                    recursive_mkdir(DIR_FS_CATALOG . $t_array[$loop_index]['PARENT_DIR'][0]['@']['NAME'] . $t_array[$loop_index]['DIR'][0]['@']['NAME']);
                    $output.="<div class=\"section\"><font class=\"section-title\">".CREATING_DIRS_IN_BOLD."</font>" . DIR_FS_CATALOG .
                                       $t_array[$loop_index]['PARENT_DIR'][0]['@']['NAME'] . "<font class=\"highlighted\">" .
                                       nl2br(htmlentities($t_array[$loop_index]['DIR'][0]['@']['NAME'])).
                                       "</font><br></div>";
                } elseif ($action=='remove') {
                    $output.="<div class=\"section\"><font class=\"section-title\">".REMOVING_DIRS_IN_BOLD."</font>" . DIR_FS_CATALOG .
                                        nl2br(htmlentities($t_array[$loop_index]['PARENT_DIR'][0]['@']['NAME'])) . "<font class=\"highlighted\">".
                                        nl2br(htmlentities($t_array[$loop_index]['DIR'][0]['@']['NAME']))."</font><br></div>";

                    $directory = DIR_FS_CATALOG . $t_array[$loop_index]['PARENT_DIR'][0]['@']['NAME'] . $t_array[$loop_index]['DIR'][0]['@']['NAME'];
                    do {
                        if(is_dir($directory)) {
                            if(@!rmdir($directory))      error("<div class=\"error\">".COLUDNT_REMOVE_DIR_TEXT .
                                        $directory." (make_dir #".($loop_index+1).")<br></div>");
                        }
                    } while (dirname(DIR_FS_CATALOG . $t_array[$loop_index]['PARENT_DIR'][0]['@']['NAME']) != dirname($directory = dirname($directory)));
                }
            }
        }
        return $output;
    }


    function del_file($t_array, $action='', $contrib='') {


        if($t_array[0]) {
            for($loop_index = 0; $loop_index<count($t_array); $loop_index++) {
                if ($action=='install') {
                    if (is_file(DIR_FS_CATALOG . $t_array[$loop_index]['FILE'][0]['@']['NAME'])) {
                        $output.="<div class=\"section\"><font class=\"section-title\">".REMOVING_FILE_TEXT."</font>".
                                        DIR_FS_CATALOG. $t_array[$loop_index]['FILE'][0]['@']['NAME']."<br></div>";
                        //save_md5 (DIR_FS_CATALOG . $t_array[$loop_index]['FILE'][0]['@']['NAME'], $contrib);
                        if(!(@ unlink(DIR_FS_CATALOG . $t_array[$loop_index]['FILE'][0]['@']['NAME']))) {
                                    error(COULDNT_REMOVE_FILE_TEXT.$t_array[$loop_index]['FILE'][0]['@']['NAME']."(del_file  #".($loop_index+1).")<br>");
                        }
                    } else    $output.="<div class=\"section\"><font class=\"section-title\">".REMOVING_FILE_TEXT."</font>
                                    <font class=\"error\">".FILE_NOT_EXISTS_TEXT.": </font>". DIR_FS_CATALOG.$t_array[$loop_index]['FILE'][0]['@']['NAME']."<br></div>";
                } elseif ($action=='remove') {
                    $output.="<div class=\"section\"><font class=\"section-title\">".COPYING_TO_TEXT."</font>".
                                        DIR_FS_CATALOG.$t_array[$loop_index]['FILE'][0]['@']['NAME'] . "<br></div>";
                    if(!is_file(DIR_FS_CATALOG . $t_array[$loop_index]['FILE'][0]['@']['NAME'])) {
                        restore_backuped_file($t_array[$loop_index]['FILE'][0]['@']['NAME']);
                        //save_md5 (DIR_FS_CATALOG . $t_array[$loop_index]['FILE'][0]['@']['NAME'], $contrib);
                    }
                }
            }
        }
        return $output;
    }










    function addfile($t_array, $action='', $contrib='') {
        if($t_array[0]) {
            for($loop_index = 0; $loop_index<count($t_array); $loop_index++) {
                if ($action=='install') {
                    $output.="<div class=\"section\"><font class=\"section-title\">".COPYING_TO_TEXT."</font>".
                                        DIR_FS_CATALOG.$t_array[$loop_index]['FILE'][0]['@']['NAME']. "</br></div>";
                    if(is_file(DIR_FS_CATALOG . $t_array[$loop_index]['FILE'][0]['@']['NAME'])) {
                        if(!(@ unlink(DIR_FS_CATALOG.$t_array[$loop_index]['FILE'][0]['@']['NAME']))) {
                            error(COULDNT_REMOVE_FILE_TEXT.DIR_FS_CATALOG.$t_array[$loop_index]['FILE'][0]['@']['NAME']." (addfile  #".($loop_index+1).")<br>");
                        }
                    }
                    if(!is_file(DIR_FS_CATALOG . $t_array[$loop_index]['FILE'][0]['@']['NAME'])) {
                        if(@!copy(MODULE_CONTRIB_INSTALLER_DIR_FS_CONTRIBUTIONS . $contrib . '/catalog/' . $t_array[$loop_index]['FILE'][0]['@']['NAME'],
                            DIR_FS_CATALOG.$t_array[$loop_index]['FILE'][0]['@']['NAME']))
                        {
                            error(COULDNT_COPY_TO_TEXT. $t_array[$loop_index]['FILE'][0]['@']['NAME']."<br>".
                                    MODULE_CONTRIB_INSTALLER_DIR_FS_CONTRIBUTIONS . $contrib . '/catalog/' . $t_array[$loop_index]['FILE'][0]['@']['NAME']." (addfile  #".($loop_index+1).")");
                        }
                        chmod(DIR_FS_CATALOG . $t_array[$loop_index]['FILE'][0]['@']['NAME'], 0775);
                        //save_md5 (DIR_FS_CATALOG . $t_array[$loop_index]['FILE'][0]['@']['NAME'], $_GET['contrib']);
                    }
//=========================================
                } elseif ($action=='remove') {
                    if (is_file(DIR_FS_CATALOG . $t_array[$loop_index]['FILE'][0]['@']['NAME'])) {
                        if(!(@ unlink(DIR_FS_CATALOG . $t_array[$loop_index]['FILE'][0]['@']['NAME']))) {
                            error(COULDNT_REMOVE_FILE_TEXT.$t_array[$loop_index]['FILE'][0]['@']['NAME']." (addfile  #".($loop_index+1).")<br>");
                        }
                        $output.="<div class=\"section\"><font class=\"section-title\">".REMOVING_FILE_TEXT."</font>".
                                    DIR_FS_CATALOG. $t_array[$loop_index]['FILE'][0]['@']['NAME']."<br></div>";
                        restore_backuped_file($t_array[$loop_index]['FILE'][0]['@']['NAME']);

                    } else     $output.="<div class=\"section\"><font class=\"section-title\">".REMOVING_FILE_TEXT."</font>
                                                    <font class=\"error\">".FILE_NOT_EXISTS_TEXT.": </font>".
                                                    DIR_FS_CATALOG.$t_array[$loop_index]['FILE'][0]['@']['NAME']."<br>"."</div>";
                }
            }
        }
        return $output;
    }


    function sql($t_array, $action='', $contrib='') {
    //echo_array($t_array);
        if($t_array[0]) {
            for($loop_index = 0; $loop_index<count($t_array); $loop_index++) {
                if ($action=='install') {
                    $output.="<div class=\"section\"><font class=\"section-title\">".RUN_SQL_QUERY_TEXT."</font><p class=\"sql_code\">".
                                        nl2br(htmlentities($t_array[$loop_index]['QUERY'][0]['#']))."</p></div>";
                    $t_array_query=contrib_tep_db_query($t_array[$loop_index]['QUERY'][0]['#']);
                } elseif ($action=='remove') {
                    $output.="<div class=\"section\"><font class=\"section-title\">".RUN_SQL_REMOVE_QUERY_TEXT."</font><p class=\"sql_code\">".
                                        nl2br(htmlentities($t_array[$loop_index]['REMOVE_QUERY'][0]['#'])) . "</p></div>";
                    $t_array_query= contrib_tep_db_query($t_array[$loop_index]['REMOVE_QUERY'][0]['#']);
                }
            }
        }
        return $output;
    }




    function findreplace($t_array, $action='', $contrib='') {
        global $_GET;
        if($t_array[0]) {
            for($loop_index = 0; $loop_index<count($t_array); $loop_index++) {
                $rep_str=linebreak_fixing(commented_string($t_array[$loop_index]['REPLACE'][0]['#'], $t_array[$loop_index]['REPLACE'][0]['@']['TYPE']));
                if ($action=='install') {
                    if(is_file(DIR_FS_CATALOG . $t_array[$loop_index]['FILE'][0]['@']['NAME'])) {
                        $output.="<div class=\"section\"><font class=\"section-title\">".FIND_REPLACE_IN_FILE_TEXT."</font>".
                                        DIR_FS_CATALOG . nl2br(htmlentities($t_array[$loop_index]['FILE'][0]['@']['NAME']))."<br>".
                                        EXPRESSION_TEXT." <p class=\"code\">".nl2br(htmlentities(unicode2win($t_array[$loop_index]['FIND'][0]['#']))). "</p>".
                                        REPLACE_WITH_TEXT." <p class=\"code\">".nl2br(htmlentities(unicode2win($t_array[$loop_index]['REPLACE'][0]['#'])))."</p><br>";


                        $old_file=file_get_contents(DIR_FS_CATALOG . $t_array[$loop_index]['FILE'][0]['@']['NAME']);

                        $count=substr_count($old_file, linebreak_fixing($t_array[$loop_index]['FIND'][0]['#']));
                        $new_file = str_replace(linebreak_fixing($t_array[$loop_index]['FIND'][0]['#']), $rep_str, $old_file);
                        if ($count>1)    $output.="<p class=\"error\">"."Replaced ".$count." times!!!"."</p>";
                        if ($count==0) {
                            $count=substr_count($old_file, linebreak_fixing(trim($t_array[$loop_index]['FIND'][0]['#'])));
                            $new_file = str_replace(linebreak_fixing(trim($t_array[$loop_index]['FIND'][0]['#'])), $rep_str, $old_file);
                            if ($count>1)    $output.="<p class=\"error\">"."Replaced ".$count." times!!!"."</p>";
                        }
                        $file_write_handle = fopen(DIR_FS_CATALOG . $t_array[$loop_index]['FILE'][0]['@']['NAME'], 'w');
                        fwrite($file_write_handle, $new_file);
                        fclose($file_write_handle);

                        for($line_num = $t_array[$loop_index]['ORIGINALLINENUMBERS'][0]['@']['START'];
                            $line_num <= $t_array[$loop_index]['ORIGINALLINENUMBERS'][0]['@']['END'];
                            $line_num++) {
                                    $line_num_insert = array('line_number' => $line_num, 'file' => $t_array[$loop_index]['FILE'][0]['@']['NAME'], 'contrib_name' => $_GET['contrib']);
                                    $line_num_insert_query = tep_db_perform('contrib_installer', $line_num_insert);
                        }
                        //save_md5 (DIR_FS_CATALOG . $t_array[$loop_index]['FILE'][0]['@']['NAME'], $_GET['contrib']);
                        $output.="</div>";
                    } else     error("<div class=\"section\"><font class=\"section-title\">".FIND_REPLACE_IN_FILE_TEXT."</font>".
                                        DIR_FS_CATALOG . $t_array[$loop_index]['FILE'][0]['@']['NAME']."<br>".FILE_NOT_EXISTS_TEXT.
                                        " (findreplace  #".($loop_index+1).")</div>");
//================================================================
                } elseif ($action=='remove') {
                        $output.="<div class=\"section\"><font class=\"section-title\">".UNDO_FIND_REPLACE_IN_FILE_TEXT."</font>".
                                                 DIR_FS_CATALOG.$t_array[$loop_index]['FILE'][0]['@']['NAME']."<br>";
                    if(file_exists(DIR_FS_CATALOG . $t_array[$loop_index]['FILE'][0]['@']['NAME'])) {
                        $old_file=file_get_contents(DIR_FS_CATALOG . $t_array[$loop_index]['FILE'][0]['@']['NAME']);

                        $count=substr_count($old_file, $rep_str);
                        $new_file=str_replace($rep_str, linebreak_fixing($t_array[$loop_index]['FIND'][0]['#']), $old_file);
                        if ($count==0)  {
                            $count=substr_count($old_file, trim($rep_str));
                            $new_file=str_replace($rep_str, linebreak_fixing(trim($t_array[$loop_index]['FIND'][0]['#'])), $old_file);
                        }
                        if ($count==0) {
                                $output.="<p class=\"error\">".COULDNT_FIND_TEXT. ": ".nl2br(htmlentities($rep_str))."</p>".
                                                    IN_THE_FILE_TEXT. DIR_FS_CATALOG.$t_array[$loop_index]['FILE'][0]['@']['NAME'];
                        } else {
                            $output.=ORIGINAL_EXPRESSION_TEXT." <p class=\"code\">".nl2br(htmlentities(unicode2win($t_array[$loop_index]['FIND'][0]['#']))). "</p>".
                                        ORIGINAL_REPLACE_WITH_TEXT." <p class=\"code\">".nl2br(htmlentities(unicode2win($t_array[$loop_index]['REPLACE'][0]['#'])))."</p>";
                            if ($count>1)    $output.="<p class=\"error\">"."Replaced ".$count." times!!!"."</p>";
                        }
                        $output.="</div>";
                        $file_write_handle = fopen(DIR_FS_CATALOG . $t_array[$loop_index]['FILE'][0]['@']['NAME'], 'w');
                        fwrite($file_write_handle, $new_file);
                        fclose($file_write_handle);


                        //Check if table exist:
                        $check_query=tep_db_query("SHOW TABLE STATUS");
                        while ($table=tep_db_fetch_array($check_query))      if ($table['Name']=='contrib_installer')  $table_exists=true;

                        if ($table_exists) {
                            if ($t_array[$loop_index]['ORIGINALLINENUMBERS'][0]['@']['TYPE'] != 'continued') {
                                for($line_num = $t_array[$loop_index]['ORIGINALLINENUMBERS'][0]['@']['START'];
                                    $line_num <= $t_array[$loop_index]['ORIGINALLINENUMBERS'][0]['@']['END'];
                                    $line_num++) {
                                    tep_db_query("delete from contrib_installer where file = '" . $t_array[$loop_index]['FILE'][0]['@']['NAME'] . "' AND line_number = '" . $line_num . "'");
                                }
                            }
                        }


                    } else $output.="<p class=\"error\">".FILE_NOT_EXISTS_TEXT.": ".DIR_FS_CATALOG .$t_array[$loop_index]['FILE'][0]['@']['NAME']."</p><br></div>";
                }
            }
        }
        return $output;
    }








    function addcode($t_array, $action='', $contrib='') {
        //
        if($t_array[0]) {
            for($loop_index = 0; $loop_index<count($t_array); $loop_index++) {
                //$add_str=commented_string($t_array[$loop_index]['ADD'][0]['#'], $t_array[$loop_index]['ADD'][0]['@']['TYPE']);
                $add_str=linebreak_fixing("\n".commented_string($t_array[$loop_index]['ADD'][0]['#'], $t_array[$loop_index]['ADD'][0]['@']['TYPE']));

                if ($action=='install') {
                    if(file_exists(DIR_FS_CATALOG . $t_array[$loop_index]['FILE'][0]['@']['NAME'])) {
                        $output.="<div class=\"section\"><font class=\"section-title\">".ADD_CODE_IN_FILE_TEXT."</font>" . DIR_FS_CATALOG .
                                        $t_array[$loop_index]['FILE'][0]['@']['NAME']."<br>".
                                        EXPRESSION_TEXT." <p class=\"add_code\">".nl2br(htmlentities(unicode2win($t_array[$loop_index]['FIND'][0]['#'])))."</p>".
                                        AFTER_EXPRESSION_ADD_TEXT." <p class=\"add_code\">".
                                            nl2br(htmlentities(unicode2win($t_array[$loop_index]['ADD'][0]['#'])))."</p><br></div>";
                        $old_file=file_get_contents(DIR_FS_CATALOG . $t_array[$loop_index]['FILE'][0]['@']['NAME']);

                        $position = strpos($old_file, unicode2win(linebreak_fixing($t_array[$loop_index]['FIND'][0]['#'])));
                        if ($position===false) {
                             $position = strpos($old_file, unicode2win(linebreak_fixing(trim($t_array[$loop_index]['FIND'][0]['#']))));
                             $position += strlen(unicode2win(linebreak_fixing(trim($t_array[$loop_index]['FIND'][0]['#']))));
                        } else {
                            $position += strlen(unicode2win(linebreak_fixing($t_array[$loop_index]['FIND'][0]['#'])));
                        }


                        $new_file = substr_replace($old_file, $add_str, $position, 0);
                        $file_write_handle = fopen(DIR_FS_CATALOG . $t_array[$loop_index]['FILE'][0]['@']['NAME'], 'w');
                        fwrite($file_write_handle, $new_file);
                        fclose($file_write_handle);

                        //save_md5 (DIR_FS_CATALOG . $t_array[$loop_index]['FILE'][0]['@']['NAME'], $_GET['contrib']);
                    } else     error("<div class=\"section\"><font class=\"section-title\">".ADD_CODE_IN_FILE_TEXT."</font>".
                                        DIR_FS_CATALOG . $t_array[$loop_index]['FILE'][0]['@']['NAME']."<br>".FILE_NOT_EXISTS_TEXT." (addcode  #".($loop_index+1).")</div>");
//================================================================
                } elseif ($action=='remove') {
                    $output.="<div class=\"section\"><font class=\"section-title\">".UNDO_ADD_CODE_IN_FILE_TEXT."</font>".
                                        DIR_FS_CATALOG. $t_array[$loop_index]['FILE'][0]['@']['NAME'] . "<br>";
                    if(is_file(DIR_FS_CATALOG . $t_array[$loop_index]['FILE'][0]['@']['NAME'])) {
                        $old_file=file_get_contents(DIR_FS_CATALOG . $t_array[$loop_index]['FILE'][0]['@']['NAME']);
                        $position=strpos($old_file, $add_str);
                        if ($position===false) {
                             $output.="<p class=\"error\">".COULDNT_FIND_TEXT.": ".nl2br(htmlentities(commented_string($t_array[$loop_index]['ADD'][0]['#'],
                                                  $t_array[$loop_index][0]['@']['ADD']['TYPE'])))."</p>".IN_THE_FILE_TEXT. DIR_FS_CATALOG .
                                                  $t_array[$loop_index]['FILE'][0]['@']['NAME']. '</div>';
                        } else {
                            $length = strlen($add_str);
                            $new_file = substr_replace($old_file, '', $position, $length);
                            $file_write_handle = fopen(DIR_FS_CATALOG . $t_array[$loop_index]['FILE'][0]['@']['NAME'], 'w');
                            fwrite($file_write_handle, $new_file);
                            fclose($file_write_handle);
                            $output.=ORIGINAL_EXPRESSION_TEXT." <p class=\"add_code\">".nl2br(htmlentities(unicode2win($t_array[$loop_index]['FIND'][0]['#']))).
                                               "</p>".ORIGINAL_AFTER_EXPRESSION_ADD_TEXT."<p class=\"add_code\">".
                                               nl2br(htmlentities(unicode2win($t_array[$loop_index]['ADD'][0]['#'])))."</p></div>";
                        }
                    }  else return "<p class=\"error\">".FILE_NOT_EXISTS_TEXT.": ".DIR_FS_CATALOG .$t_array[$loop_index]['FILE'][0]['@']['NAME']."</p></div>";
                }
            }
        }
        return $output;
    }

?>